// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

#include <cantor/parser/parser.h>
#include <cantor/transform/dqls/indirection.h>
#include <cantor/transform/expr.h>


namespace cantor::transform {

    void TransIndirection::resolve_index(turbo::Nonnull<const cantor::parser::Indirection *> node) {
        auto ptr = (cantor::parser::IndirectionIndex*)node;
        if (ptr->index_expr) {
            status = TransExpr::resolve_to(ptr->index_expr, indirection.mutable_index()->mutable_expr());
        } else {
            status = turbo::data_loss_error("missing index expression");
        }
    }

    void TransIndirection::resolve_slice(turbo::Nonnull<const cantor::parser::Indirection *> node) {
        auto ptr = (cantor::parser::IndirectionSlice*)node;
        auto slice  = indirection.mutable_slice();
        if (ptr->slice_low) {
            status = TransExpr::resolve_to(ptr->slice_low, slice->mutable_low());
        }
        if (!status.ok()) {
            return;
        }
        if (ptr->slice_high) {
            status = TransExpr::resolve_to(ptr->slice_high, slice->mutable_high());
        }
        if (!status.ok()) {
            return;
        }
        if (ptr->slice_step) {
            status = TransExpr::resolve_to(ptr->slice_step, slice->mutable_step());
        }
    }

    void TransIndirection::resolve_ast(const cantor::parser::Indirection *node) {
        if (!node) {
            status = turbo::data_loss_error("no expr");
            return;
        }
        indirection.set_type(node->indirection_type);
        switch (node->indirection_type) {
            case pb::ast::IND_SLICE: {
                resolve_slice(node);
                break;
            }
            case pb::ast::IND_INDEX: {
                resolve_index(node);
                break;
            }
            case pb::ast::IND_NONE:
            default:
                status = turbo::invalid_argument_error("unknown indirection type",
                                                       static_cast<int>(node->indirection_type));
        }
        if (!status.ok()) {
            return;
        }

        set_proto_location(node, indirection.mutable_location());
        set_proto_status(indirection.mutable_status());
    }

    const turbo::Status &TransIndirection::check() {
        if (!status.ok()) {
            return status;
        }
        if (!indirection.has_location()) {
            make_missing_required_field("location");
            return status;
        }
        return status;
    }

    turbo::Status TransIndirection::resolve_to(turbo::Nonnull<const cantor::parser::Indirection *> arg,
                                        turbo::Nonnull<pb::ast::Indirection *> out) {
        TransIndirection trans;
        trans.resolve_ast(arg);
        auto status = trans.check();
        if (!status.ok()) {
            return status;
        }
        *out = std::move(trans.indirection);
        return status;
    }
} // namespace cantor::transform
